package com.meanbean.studytime;

import java.io.File;

import com.meanbean.studytime.controls.DialogResult;
import com.meanbean.studytime.controls.StudyControl;
import com.meanbean.studytime.controls.StudyTimeMenuBar;

import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

/**
 * A Stage used to display a StudyControl and an EditControl.
 * @author Derek Webb
 *
 */
public class StudyTimeStage extends Stage	
{
	private VBox root;
	private static StudyTimeMenuBar studyTimeMenuBar;
	private static StudyControl studyControl;
	
	public static enum Mode { Study, Edit };
	private static Mode mode;
	
	/**
	 * Creates a StudyTimeStage object.
	 * @throws Exception 
	 */
	public StudyTimeStage(String studyTimeFileString) throws Exception
	{
		super(StageStyle.DECORATED);
		
		// Create the root VBox
		root = new VBox();
		
		// Set the scene with the root
		setScene(new Scene(root));
		
		// Create the controls
		studyTimeMenuBar = new StudyTimeMenuBar(this);
		studyControl = new StudyControl();
		
		// add the StudyTimeMenuBar
		root.getChildren().add(studyTimeMenuBar);
		
		// Add the StudyControl
		root.getChildren().add(studyControl);
		
		// Set the studyControl to grow when the window is resized
		VBox.setVgrow(studyControl, Priority.ALWAYS);
		
		// Set the size of the StudyTimeStage
		this.setWidth(800);
		this.setHeight(600);
		
		// Listen to the Window Close Request event
		this.setOnCloseRequest(new EventHandler<WindowEvent>()
		{
			@Override
			public void handle(WindowEvent windowEvent)
			{
				if (mode == Mode.Edit)
					studyControl.saveButtons();
				
				if (studyControl.needsToSaveSTF())
				{
					try
					{
						DialogResult dialog = new DialogResult("Save Study Time File?", "Would you like to save your changes?");
						dialog.initOwner(StudyTimeMain.studyTimeStage);
						dialog.showAndWait();
						
						if (dialog.getResult() == DialogResult.Result.Yes)
							studyControl.saveSTF();
						else if (dialog.getResult() == DialogResult.Result.Cancel)
							windowEvent.consume();
						
					} catch (Exception e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		
		// Set the title
		setTitle("Study Time " + StudyTimeMain.VERSION);
        
		// If the command line arguments are not null, then open the provided STF file
		if (studyTimeFileString != null)
		{
			File file = new File(studyTimeFileString);
	        StudyTimeFile stf = StudyTimeFile.createWithStudyTimeFile(file.toPath());
	        openSTF(stf);
		}
	}
	
	/**
	 * Sets the mode of the program.
	 * @param newMode The mode to change to.
	 */
	public static void setMode(Mode newMode)
	{
		// If the mode has changed
		if (mode != newMode)
		{
			mode = newMode;
			studyControl.modeChanged();
			
			if (mode == Mode.Study)
			{
				studyTimeMenuBar.setModeLabelText("Mode: Study");
				studyTimeMenuBar.disableResetMenuItem(false);
			}
			else if (mode == Mode.Edit)
			{
				studyTimeMenuBar.setModeLabelText("Mode: Edit");
				studyTimeMenuBar.disableResetMenuItem(true);
			}
		}
	}
	
	/**
	 * @return The current mode (either Study or Edit).
	 */
	public static Mode getMode()
	{
		return mode;
	}
	
	/**
	 * Opens a new STF file with the StudyControl.
	 * @param studyTimeFile The StudyTimeFile to open.
	 */
	public void openNewSTF(StudyTimeFile studyTimeFile)
	{
		studyControl.openNewSTF(studyTimeFile);
	}
	
	/**
	 * Opens an already existing STF file with the StudyControl.
	 * @param studyTimeFile The StudyTimeFile to open.
	 */
	public void openSTF(StudyTimeFile studyTimeFile)
	{
		studyControl.openSTF(studyTimeFile);
	}
	
	/**
	 * @return The StudyControl associated with the StudyTimeStage.
	 */
	public static StudyControl getStudyControl()
	{
		return studyControl;
	}
}
